package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PutMapping;


import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;


/**
 * 工作台service层
 */
@Service
@Slf4j
public class WorkSpaceServiceImpl implements WorkSpaceService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 今日运营数据
     *
     * @return
     */
    public BusinessDataVO getTodayData(LocalDateTime begin, LocalDateTime end) {
        /**
         * 营业额：当日已完成订单的总金额
         * 有效订单：当日已完成订单的数量
         * 订单完成率：有效订单数 / 总订单数
         * 平均客单价：营业额 / 有效订单数
         * 新增用户：当日新增用户的数量
         */
//        //获取今日日期
//        LocalDate Today = LocalDate.now();
//        //获取今日零点
//        LocalDateTime beginTime = LocalDateTime.of(Today, LocalTime.MIN);
//        //获取今日12点
//        LocalDateTime endTime = LocalDateTime.of(Today, LocalTime.MAX);

        //查询今日新增用户
        //select count(id) form user where create_time > ? and create_time < ?
        Map map = new HashMap();
        map.put("begin", begin);
        map.put("end", end);
        Integer newUser = userMapper.getUserSumByDate(map);

        //查询总订单数
        Integer sumOrders = orderMapper.countOrders(map);
        //查询有效订单数(已完成订单)
        map.put("status", Orders.COMPLETED);
        Integer SumValidOrders = orderMapper.countOrders(map);
        //声明订单完成率和平均客单价
        Double orderCompletionRate = 0.0;
        Double unitPrice = 0.0;
        //计算今日总营业额
        Double turnover = orderMapper.getSumByDate(map);
        turnover = turnover == null ? 0 : turnover;
        if (sumOrders != 0 && SumValidOrders != 0) {
            //订单完成率
            orderCompletionRate = SumValidOrders.doubleValue() / sumOrders;
            //平均客单价
            unitPrice = turnover / SumValidOrders;
        }

        return BusinessDataVO.builder()
                .validOrderCount(SumValidOrders)
                .turnover(turnover)
                .orderCompletionRate(orderCompletionRate)
                .newUsers(newUser)
                .unitPrice(unitPrice)
                .build();
    }

    /**
     * 订单管理统计
     *
     * @param begin
     * @param end
     * @return
     */
    public OrderOverViewVO getOrderData(LocalDateTime begin, LocalDateTime end) {
        Map map = new HashMap();
        map.put("begin", begin);
        map.put("end", end);

        //全部订单
        Integer allOrders = orderMapper.countOrders(map);

        //待接单
        map.put("status", Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = orderMapper.countOrders(map);
        //待派送
        map.put("status", Orders.DELIVERY_IN_PROGRESS);
        Integer deliveredOrders = orderMapper.countOrders(map);
        //已取消
        map.put("status", Orders.CANCELLED);
        Integer cancelledOrders = orderMapper.countOrders(map);
        //已完成
        map.put("status", Orders.COMPLETED);
        Integer completedOrders = orderMapper.countOrders(map);

        return OrderOverViewVO.builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .completedOrders(completedOrders)
                .build();
    }

    /**
     * 查询菜品总览
     *
     * @param begin
     * @param end
     * @return
     */
    public DishOverViewVO dishOverView(LocalDateTime begin, LocalDateTime end) {
        //查询菜品中起售和已停售的菜品数量
        Map map = new HashMap();
        map.put("status",StatusConstant.ENABLE);
        Integer sold = dishMapper.countByStatus(map);
        map.put("status",StatusConstant.DISABLE);
        Integer discontinued = dishMapper.countByStatus(map);
        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     * 查询套餐总览
     *
     * @param begin
     * @param end
     * @return
     */
    public SetmealOverViewVO setmealOverView(LocalDateTime begin, LocalDateTime end) {
        Map map = new HashMap();
        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = setmealMapper.countByMap(map);
        map.put("status", StatusConstant.ENABLE);
        Integer sold = setmealMapper.countByMap(map);
        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }
}
